{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "error",
     "evalue": "OpenCV(4.3.0) C:\\projects\\opencv-python\\opencv\\modules\\highgui\\src\\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-0e988519f7aa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[1;31m# 读取一帧数据\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mret\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcap\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'frame'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mframe\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m     \u001b[1;31m# waitKey功能是不断刷新图像，单位ms，返回值是当前键盘按键值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[1;31m# ord返回对应的ASCII数值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31merror\u001b[0m: OpenCV(4.3.0) C:\\projects\\opencv-python\\opencv\\modules\\highgui\\src\\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "# 从摄像头获取图像数据\n",
    "cap = cv2.VideoCapture(0)\n",
    "\n",
    "while(True):\n",
    "    # ret 读取成功True或失败False\n",
    "    # frame读取到的图像的内容\n",
    "    # 读取一帧数据\n",
    "    ret,frame = cap.read()\n",
    "    cv2.imshow('frame',frame)\n",
    "    # waitKey功能是不断刷新图像，单位ms，返回值是当前键盘按键值\n",
    "    # ord返回对应的ASCII数值\n",
    "    for i in range(10): \n",
    "        cv2.imwrite('C:\\\\Users\\\\DELL\\\\Desktop\\\\opencv-test\\\\imgaed%04d.jpg'%(i),frame)\n",
    "    if cv2.waitKey(1) & 0xff == ord('q'):\n",
    "        break\n",
    "# 释放摄像头资源        \n",
    "cap.release()\n",
    "# 销毁显示窗口\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.0\n",
      "(640, 480)\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "\n",
    "cameraCapture = cv2.VideoCapture(0)\n",
    "fps = cameraCapture.get(cv2.CAP_PROP_FPS)\n",
    "print(fps)#这里打印出来，就是为了查看参数是否获取成功。\n",
    "\n",
    "size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))\n",
    "print(size)#这里打印出来，就是为了查看参数是否获取成功。\n",
    "\n",
    "vidoWriter = cv2.VideoWriter('C:\\\\Users\\\\DELL\\\\Desktop\\\\opencv-test\\\\Myrecord.mp4', cv2.VideoWriter_fourcc('I', '4', '2', '0'), fps, size)\n",
    "\n",
    "success, frame = cameraCapture.read()\n",
    "numFramesRemaining = 10 * fps -1 #录制大约10秒\n",
    "\n",
    "while success and numFramesRemaining > 0 :\n",
    "    vidoWriter.write(frame)\n",
    "    success, frame = cameraCapture.read()\n",
    "    numFramesRemaining -= 1\n",
    "cameraCapture.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "fourcc = cv2.VideoWriter_fourcc(*'XVID')\n",
    "out = cv2.VideoWriter('C:\\\\Users\\\\DELL\\\\Desktop\\\\opencv-test\\\\output.avi',fourcc,fps,(frame_width,frame_height))\n",
    "\n",
    "while(True):\n",
    "    ret, frame = cap.read()\n",
    "    if ret==True:\n",
    "        # 水平翻转\n",
    "        frame = cv2.flip(frame,1)\n",
    "        out.write(frame)\n",
    "        cv2.imshow('frame',frame)\n",
    "        if cv2.waitKey(25) & 0xff == ord('q'):\n",
    "            break\n",
    "    else:\n",
    "        break\n",
    "out.release()\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
